/**
* @author DuanSky
* @date 2015年9月11日 下午1:54:37
* @content 
*/
package com.sky.learningJava.bytecode;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class DynamicCompiler {
	public static void main(String[] args) throws Exception {      
	      String source = "public class Main "
	      		+ "{ "
	      		+ "public static void main(String[] args) "
	      		+ "{System.out.println(\"Hello World!\");} "
	      		+ "}";
	      JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
	      StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
	     
	      StringSourceJavaObject sourceObject = new DynamicCompiler.StringSourceJavaObject("Main", source);
	      Iterable<JavaFileObject> fileObjects = Arrays.asList(sourceObject);
	      CompilationTask task = compiler.getTask(null, fileManager, null, null, null, fileObjects);
	      boolean result = task.call();
	      if (result) {
	         System.out.println("编译成功。");
	      }
	   }
	
	static class StringSourceJavaObject extends SimpleJavaFileObject {

	      private String content = null;
	      public StringSourceJavaObject(String name, String content) throws URISyntaxException {
	         super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE);
	         this.content = content;
	      }

	      public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
	         return content;
	      }
	   }


}
